import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;


public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true) {
			String str = br.readLine();
			if(str.trim().equals("0")) {
				break;
			}
			StringTokenizer sToken = new StringTokenizer(str);
			int n = Integer.parseInt(sToken.nextToken());
			long[] hist = new long[n];
			int index = 0;
			while(sToken.hasMoreTokens()) {
				hist[index++] = Long.parseLong(sToken.nextToken());
			}
			System.out.println(maxArea(hist));
		}
		
	}
	
	public static long maxArea(long[] hist) {
		long[] histogram = new long[hist.length + 2];
		histogram[0] = 0;
		for(int i = 0; i < hist.length; i++) {
			histogram[i + 1] = hist[i];
		}
		histogram[hist.length + 1] = 0;
		return maxArea1(histogram);
	}
	
	
	public static long maxArea1(long[] hist) {
		Stack<HistogramCandidate> stack = new Stack<HistogramCandidate>();
		stack.push(new HistogramCandidate(0, 0));
		long maxArea = 0;
		for(int i = 1; i < hist.length; i++) {
			maxArea = Math.max(maxArea, pushCandidate(stack, hist, i));
		}
		return maxArea;
	}
	
	public static long pushCandidate(Stack<HistogramCandidate> stack, long[] hist, int index) {
		HistogramCandidate topCandidate = stack.peek();
		long maxArea = 0;
		while(topCandidate.height > hist[index]) {
			HistogramCandidate solve = stack.pop();
			topCandidate = stack.peek();
			long leftArea = (solve.index - topCandidate.index -1) * solve.height;
			long rightArea = (index - solve.index - 1) * solve.height;
			long totalArea = leftArea + rightArea + solve.height;
			maxArea = Math.max(maxArea, totalArea);
		}
		stack.push(new HistogramCandidate(hist[index], index));
		return maxArea;
	}
	
	private static final class HistogramCandidate {
		
		private final long height;
		
		private final long index;
		
		public HistogramCandidate(long h, long i) {
			this.height = h;
			this.index = i;
		}
		
	}

}

